<?php
namespace app\admin\model;

use think\Model;
use think\Request;

class Brand  extends Model{

    private $brandModel;
    public function __construct(){
        parent::__construct();
        $this -> brandModel = db('brand');
    }

    /*  发布项目
     *  @param Array [save]
     *  @return Id
     * **/
    public  function add($save){
        $result = $this -> brandModel -> insertGetId($save);
        return $result;
    }

    /*  品牌详情
     *
     * **/
    public function getDataInfo($where){
        $data = $this -> brandModel -> where($where) -> find();
        if(empty($data)){   return [];  }
        $data['image'] = explode(',',trim($data['image'],','));
        //格式化数据
        return $data;
    }


    /*  分页获取数据
     *  @param Obj [where,order,num,&page,type]
     * **/
    public function getBrandListPageByWhere( $where  = false , $order = false , $num , &$page , $type = 1){
        $brandData = $this -> brandModel -> where($where) -> order($order) -> paginate($num, false, ['query' => Request::instance()->param(),]);
        $page = $brandData->render();
        $data = iterator_to_array($brandData);

        if(empty($data)){   return [];  }
        switch ($type) {
            case 1:
                $dataList = $this -> formatBrandDataforList($data);
                break;

            default:
                # code...
                break;
        }
        return $dataList;
    }

    /*  品牌列表 格式化数据
     *  @param Array [data]
     *  @return Array [datalist]
     * **/
    public function formatBrandDataforList($data){
        foreach ($data as $key => $value) {
            $dataList[$key]['id'] = $value['id'];
            $dataList[$key]['title'] = $value['title'];
            $dataList[$key]['subtitle'] = $value['subtitle'];
            $dataList[$key]['img'] = $value['img'];
            $dataList[$key]['sign'] = $value['sign'];
            $dataList[$key]['phone'] = $value['phone'];
            $dataList[$key]['scope'] = $value['scope'];
            $dataList[$key]['order'] = $value['order'];
            $dataList[$key]['reco_index'] = $value['reco_index'];
            $dataList[$key]['reco_header'] = $value['reco_header'];
            $dataList[$key]['addtime'] = date('Y-m-d H:i:s',$value['addtime']);
            $dataList[$key]['read_num'] = $value['read_num'];
        }
        return $dataList;
    }
    /*  获取品牌列表
     *  @param  Array   [where]
     *  @param  string  'order'
     *  @param  string  'limit'
     *  @param  int     type
     *  @return Array   [datalist]
     * **/
    public function getBrandDataForListByWhere($where , $order = 'id desc' ,$limit = false ,$type = 1 ){
        $data = $this -> brandModel -> where($where) -> order($order) -> limit($limit) -> select();
        $dataList = $this -> formatBrandListForDetail($data);
        return $dataList;
    }
    /*  格式化数据
     *  @param  Array   [data]
     *  @return Array   [datalist]
     *  **/
    public function formatBrandListForDetail($data){
        foreach ($data as $key => $value) {
            $dataList[$key]['id'] = $value['id'];
            $company = json_decode($value['company'],true);
            $desc = json_decode($value['desc'],true);
            if(empty($company)){
                $dataList[$key]['company'] = json_decode($value['company'].'}',true);
            }
            if(empty($desc)){
                $dataList[$key]['desc'] = json_decode($value['desc'].'}',true);
            }
            $dataList[$key]['company'] = $company;
            $dataList[$key]['desc'] = $desc;
            if($dataList[$key]['company']){
                $dataList[$key]['company']['money'] = str_replace('万','',$dataList[$key]['company']['money']);
            }
            if($dataList[$key]['desc']){
                $dataList[$key]['desc']['money'] = str_replace('万','',$dataList[$key]['desc']['money']);
                $dataList[$key]['desc']['tal'] = str_replace('万','',$dataList[$key]['desc']['tal']);
            }
        }
        return $dataList;
    }


    /*  修改信息
     *  @param field [field]
     *  @return res
     * **/
    public function modify($where = false , $save = false ){
        $result = $this -> brandModel -> where($where) -> update($save);
        return $result;
    }



}